import { validateCustomTwoDecimal, validateCustomPositiveInt } from '@/utils/validate'
// 表单： 如果启用 顾客奖励 必须选择其中一种
function validateMemberType(rule, value, callback) {
  if (this.form.enableMemberBonus && value.length === 0) {
    callback(new Error('请至少选择一种顾客奖励方式'))
  } else {
    callback()
  }
}
//  顾客奖励余额
function validateBalanceBonus(rule, value, callback) {
  if (
    !this.form.enableMemberBonus || !this.form.memberConfigVos[this.activeName].memberType.includes('balance')) {
    callback()
  } else {
    if (this.$isNull(value)) {
      callback(new Error('请输入赠送余额'))
    } else if (!validateCustomTwoDecimal(value, 7)) {
      callback(new Error('请输入0.01~9999999.99'))
    } else {
      callback()
    }
  }
}
//  顾客奖励积分
function validatePointsBonus(rule, value, callback) {
  if (
    !this.form.enableMemberBonus ||
        !this.form.memberConfigVos[this.activeName].memberType.includes('points')
  ) {
    callback()
  } else {
    if (this.$isNull(value)) {
      callback(new Error('请输入赠送积分'))
    } else if (!validateCustomPositiveInt(value, 7)) {
      callback(new Error('请输入正整数'))
    } else {
      callback()
    }
  }
}
// 奖励优惠券
function validateCouponBonus(rule, value, callback) {
  if (!value.length) {
    callback('请至少选择一种')
  } else if (value.length > 10) {
    callback('卡券种类不超过10种')
  } else {
    let maxAmount = 0
    value.forEach(v => {
      if (validateCustomPositiveInt(v.amount, 2)) {
        maxAmount += v.amount
      }
    })
    if (maxAmount > 30) {
      callback('卡券总数量不超过30张')
    } else {
      callback()
    }
  }
}
// 限制设置
function validateLimitAmount(rule, value, callback) {
  const curTab = this.form.memberConfigVos.find(i => i.name === this.activeName)
  if (curTab && curTab.openLimitSetting) {
    if (this.$isNull(value)) {
      callback(new Error('请输入限制金额'))
    } else if (!validateCustomPositiveInt(value, 0)) {
      callback(new Error('请输入正整数'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}
// 奖励优惠券
function validateCouponBonusAmount(rule, value, callback) {
  console.log(value)
  if (this.$isNull(value)) {
    callback('请输入数量')
  } else if (!validateCustomPositiveInt(value, 2)) {
    callback(new Error('请输入正整数'))
  } else {
    this.$refs.form.validateField(['couponBonus'])
    callback()
  }
}

export default {
  validateMemberType,
  validateBalanceBonus,
  validatePointsBonus,
  validateCouponBonus,
  validateLimitAmount,
  validateCouponBonusAmount
}
